#!bin/bash

function senario_4k_read() {
    for i in {1..2};do
        sleep 30
        mkdir 4k_read$1$2
        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=128 -rw=randread -ioengine=libaio -bs=4k \
        -size=1G -numjobs=$1 -runtime=300 -time_based -group_reporting -name=randread \
        > ./4k_read$1$2/task_randread_4K_128depths_1threads_file$i.fioresult


        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time| grep "Sys_Average_Watt" >> 4k_randread_power$i.txt

        rm -f ./localpwrinfo.txt
        mv 4k_randread_power$i.txt ./4k_read$1$2

    done
}


function senario_4k_write() {
    for i in {1..2};do
        sleep 30
        mkdir 4k_write$1$2

        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=128 -rw=randwrite -ioengine=libaio -bs=4k \
        -size=1G -numjobs=$1 -runtime=300 -time_based -group_reporting -name=randwrite \
        > ./4k_write$1$2/task_randwrite_4K_128depths_1threads_file$i.fioresult


        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time | grep "Sys_Average_Watt" >> 4k_randwrite_power$i.txt
        rm -f ./localpwrinfo.txt
        mv 4k_randwrite_power$i.txt ./4k_write$1$2
    done
}

function senario_4k_ReadLatency() {
    for i in {1..2};do
        sleep 60
        mkdir 4k_readLatency$1$2

        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=1 -rw=randread -ioengine=libaio -bs=4k \
        -size=1G -numjobs=$1 -group_reporting -name=Rand_read_Latency_Testing -runtime=300 -time_based \
        > ./4k_readLatency$1$2/task_read_latency_4K_1depths_1threads_file$i.fioresult

        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time |  grep "Sys_Average_Watt" >> 4k_ReadLatency_power$i.txt
        rm -f ./localpwrinfo.txt
        mv 4k_ReadLatency_power$i.txt ./4k_readLatency$1$2
    done
}

function senario_4k_WriteLatency() {
    for i in {1..2};do
        sleep 60
        mkdir 4k_WriteLatency$1$2

        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=1 -rw=randwrite -ioengine=libaio -bs=4k \
        -size=1G -numjobs=$1 -group_reporting -name=Rand_Write_Latency_Testing -runtime=300 -time_based \
        > ./4k_WriteLatency$1$2/task_write_latency_4K_1depths_1threads_file$i.fioresult

        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time | grep "Sys_Average_Watt" >> 4k_WriteLatency_power$i.txt
        rm -f ./localpwrinfo.txt
        mv 4k_WriteLatency_power$i.txt ./4k_WriteLatency$1$2
    done
}

function senario_1024k_write() {
    for i in {1..2};do
        sleep 60
        mkdir 1024k_write$1$2

        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=64 -rw=write -ioengine=libaio -bs=1024k \
        -size=1G -numjobs=$1 -runtime=300 -group_reporting -name=Write_BW_Test -time_based=1 \
        > ./1024k_write$1$2/task_write_1024K_64depths_1threads_file$i.fioresult

        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time| grep "Sys_Average_Watt"  >> 1024k_write_power$i.txt
        rm -f ./localpwrinfo.txt
        mv 1024k_write_power$i.txt ./1024k_write$1$2
    done
}

function senario_1024k_read() {
    for i in {1..2};do
        sleep 60
        mkdir 1024k_read$1$2
        sh ./powertool.sh collect 1 &

        start_time=$(date +"%m.%d %H:%M:%S");
            #echo "stime:" $start_time
        fio -filename=/home/fio_test/fio -direct=1 -iodepth=64 -rw=read -ioengine=libaio -bs=1024k \
        -size=1G -numjobs=$1 -runtime=300 -group_reporting -name=Read_BW_Test -time_based=1 \
        > ./1024k_read$1$2/task_read_1024K_64depths_1threads_file$i.fioresult

        end_time=$(date +"%m.%d %H:%M:%S");
        echo "etime:" $end_time

        kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
        sh ./powertool.sh avg $start_time $end_time | grep "Sys_Average_Watt" >> 1024k_read_power$i.txt
        rm -f ./localpwrinfo.txt

        mv 1024k_read_power$i.txt ./1024k_read$1$2
    done

}

load_level_latency=("265" "280" "290" "310" "325" "370")
load_level_4k=("10" "20" "40" "60" "80" "100")


load_level_1024k=("10" "20" "28" "35" "45" "100")
cpu_usage=("10" "20" "30" "40" "50" "70")
governor_list=("seep" "performance")


function test_1024k_write() {
    for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_1024k_write ${load_level_1024k[$i]} ${governor_list[$j]}
    done
}

function test_1024k_read() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_1024k_read ${load_level_1024k[$i]} ${governor_list[$j]}
    done
}

function test_4k_read() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";
        senario_4k_read ${load_level_4k[$i]} ${governor_list[$j]}
    done
}

function test_4k_write() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_4k_write ${load_level_4k[$i]} ${governor_list[$j]}
    done
}


function test_4k_write() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_4k_write ${load_level_4k[$i]} ${governor_list[$j]}
    done
}


function test_4k_ReadLatency() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_4k_ReadLatency ${load_level_latency[$i]} ${governor_list[$j]}
    done
}

function test_4k_WriteLatency() {
        for i in {0..5}
    do
        echo "load level:" ${cpu_usage[$i]} "start testing=================================================";

        senario_4k_WriteLatency ${load_level_latency[$i]} ${governor_list[$j]}
    done
}

for j in {0..1}
do


   cpupower  frequency-set -g ${governor_list[$j]}
   echo "1024K write  testing===========================================================================";

   test_1024k_write;


   echo "1024K read testing===========================================================================";
   test_1024k_read;


   echo "4K read testing===========================================================================";
   test_4k_read;

   echo "4K write testing===========================================================================";
   test_4k_write;

   echo "4K readlatency testing===========================================================================";
   test_4k_ReadLatency;

   echo "4K writelatency testing===========================================================================";
   test_4k_WriteLatency;
done
